package com.lczc.soa.controller;


import com.alibaba.fastjson.JSONObject;
import com.lczc.soa.domain.Suggest;
import com.lczc.soa.domain.User;
import com.lczc.soa.service.SuggestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/Suggest")
public class SuggestController {

    @Autowired
    private SuggestService suggestService;

    @RequestMapping("/contact")
    public String contactUser(Model model){
        return "user_contact";
    }
    @RequestMapping("/toadd")
    public String addquesetion(Model model){
        return "suggest_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public  String addSuggest(Suggest suggest, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("current_user");
        suggest.setUserId(user.getUserId());
        suggest.setUserName(user.getUserName());
        suggest.setCreateDate(new Date());
        suggest.setContent(request.getAttribute("content").toString());
        int insert= suggestService.saveSuggest(suggest);
        JSONObject jsonObject = new JSONObject();
        if (insert>0){
            jsonObject.put("code","0000");
            jsonObject.put("msg","保存成功！");
            return jsonObject.toJSONString();
        }
        jsonObject.put("code","9999");
        jsonObject.put("msg","保存失败！");
        return jsonObject.toJSONString();
//        return "redirect:/Suggest/toadd";
    }
}
